package com.mcr.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcr.reggie.common.R;
import com.mcr.reggie.dto.SetmealDto;
import com.mcr.reggie.entity.Setmeal;
import com.mcr.reggie.service.CategoryService;
import com.mcr.reggie.service.SetmealDishService;
import com.mcr.reggie.service.SetmealService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CacheManager cacheManager;
    /*
     *
     *
    ***************************************************** 后台 *******************************************************
     *
     *
     */
    @ApiOperation(value = "新增套餐")
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmealAndSetmealDish(setmealDto);
        return R.success("插入成功");
    }
    @ApiOperation(value = "套餐分页查询")
    @GetMapping(value = "/page")
    public R<Page> page(@RequestParam(value = "page",required = true)int page,
                        @RequestParam(value = "pageSize",required=true)int pageSize,
                        @RequestParam(value = "name",required = false)String name){
        Page<SetmealDto> setmealDtoPage=setmealService.getPage(page,pageSize,name);
        if(setmealDtoPage==null){
            return R.error("查不到此套餐");
        }
        return R.success(setmealDtoPage);
    }
    @ApiOperation(value = "删除套餐（批量or单独）")
    @DeleteMapping
    public R<String> delete(@RequestParam("ids")List<Long> ids){
        setmealService.deleteSetmealAndSetmealDish(ids);
        return R.success("套餐数据删除成功！！！");
    }
    @ApiOperation(value = "得到要修改的套餐数据")
    @GetMapping(value = "/{id}")
    public R<SetmealDto> getSetmealDtoById(@PathVariable("id")Long id){
        SetmealDto setmealDto=setmealService.getSetmealDtoById(id);
        return R.success(setmealDto);
    }
    @ApiOperation(value = "修改套餐数据")
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateData(setmealDto);
        return R.success("套餐信息修改成功");
    }
    @ApiOperation(value = "修改套餐状态")
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable("status")Integer status,String[] ids){
        setmealService.updateStatus(status,ids);
        String msg=new String();
        if(status==0){
            msg="套餐停售成功";
        }else {
            msg="套餐起售成功";
        }
        return R.success(msg);
    }
    /*
     *
     *
     ***************************************************** 前台 *******************************************************
     *
     *
     */
    @ApiOperation(value = "前台展示")
    @GetMapping(value = "/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        List<Setmeal> setmealList=setmealService.getSetmealList(setmeal);
        return R.success(setmealList);
    }
}
